import AMapPositionPicker from './AMapPositionPicker.vue';
import convertor from '../../../utils/convertor.js';
import { getLngLat } from '../js/AMapUtils.js';
import { tap } from '../../../utils/index.js';

export default {
  name: 'AMapMarkerPicker',
  extends: AMapPositionPicker,
  methods: {
    setMarkerByValue(value) {
      value = tap(this.formatInput, value);
      const { fields } = this;

      //百度地址 -> 高德地址
      let gdPositionObj;
      if (value[fields.position]) {
        let baiduPositionList = value[fields.position].split(',');
        let gdPositionList = convertor.bd2gd(baiduPositionList[0], baiduPositionList[1]);
        gdPositionObj = gdPositionList.join(',');
      }

      const position = getLngLat(gdPositionObj);
      this.address = value[fields.address];

      if (position) {
        this.addMarker(position);
      }
    },
    onChange({ position, address }) {
      const { formatOutput, fields } = this;
      let baiduPosition = convertor.gd2bd(position.lng, position.lat);
      position.Q = baiduPosition[1];
      position.R = baiduPosition[0];
      position.lng = baiduPosition[0];
      position.lat = baiduPosition[1];
      const data = tap(formatOutput, {
        [fields.position]: position,
        [fields.address]: address,
      });
      this.$emit('update:modelValue', data);
      setTimeout(() => {
        this.$emit('change', data);
      }, 0);
    },
  },
  render() {
    return '';
  },
};
